package com.shaoshaohuo.app.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.CarTypeSecondListAdapter;
import com.shaoshaohuo.app.constant.CarOption;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.entity.CarTypeFirst;
import com.shaoshaohuo.app.entity.CarTypeSecond;
import com.shaoshaohuo.app.entity.SearchCar;
import com.shaoshaohuo.app.entity.SearchGoods;
import com.shaoshaohuo.app.utils.PoiEntityUtils;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cao on 2016/1/12.
 */
public class SearchGoodsActivity extends BaseActivity implements View.OnClickListener {

    private TopbarView topbar;

    private View layout_shifadi;
    private View layout_mudidi;
    private View layout_type;
    private View layout_sort;
    private TextView textview_shifadi;
    private TextView textview_mudidi;
    private TextView textview_type;
    private TextView textview_sort;

    private CascadingMenuPopWindow cascadingMenuPopWindowStart;
    private CascadingMenuPopWindow cascadingMenuPopWindowEnd;

    private SearchGoods searchGoods = new SearchGoods();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_goods);
        initView();
        setUpView();
        initData();
    }


    private void initView() {
        topbar = (TopbarView) findViewById(R.id.topbar);

        layout_shifadi = findViewById(R.id.layout_shifadi);
        layout_mudidi = findViewById(R.id.layout_mudidi);
        layout_type = findViewById(R.id.layout_type);
        layout_sort = findViewById(R.id.layout_sort);

        textview_shifadi = (TextView) findViewById(R.id.textview_shifadi);
        textview_mudidi = (TextView) findViewById(R.id.textview_mudidi);
        textview_type = (TextView) findViewById(R.id.textview_type);
        textview_sort = (TextView) findViewById(R.id.textview_sort);
    }

    private void setUpView() {
        topbar.setCenterText("筛选条件");
        topbar.setLeftView(true, true);
        topbar.setRightText("确定");
        topbar.setRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(ExtraName.searchGoods, searchGoods);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

        layout_shifadi.setOnClickListener(this);
        layout_mudidi.setOnClickListener(this);
        layout_type.setOnClickListener(this);
        layout_sort.setOnClickListener(this);
    }


    private void initData() {
        Intent intent = getIntent();
        searchGoods = (SearchGoods) intent.getSerializableExtra(ExtraName.searchGoods);
        if (searchGoods == null) {
            searchGoods = new SearchGoods();
        } else {
            if (!TextUtils.isEmpty(searchGoods.origincityid))
                textview_shifadi.setText(searchGoods.originName);
            if (!TextUtils.isEmpty(searchGoods.receivecityid))
                textview_mudidi.setText(searchGoods.receiveName);
            if (searchGoods.shipptype != 0)
                textview_type.setText(searchGoods.shippName);
            if (searchGoods.ranktype != 0)
                textview_sort.setText(searchGoods.rankName);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_shifadi:
                if (cascadingMenuPopWindowStart == null) {
                    //向三级menu添加地区数据
                    cascadingMenuPopWindowStart = new CascadingMenuPopWindow(this, false);
                    cascadingMenuPopWindowStart
                            .setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {
                                @Override
                                public void getValue(Area city, String fullname) {
                                    textview_shifadi.setText(fullname);
                                    searchGoods.origincityid = PoiEntityUtils.convert(city, fullname).getId();
                                    searchGoods.originName = fullname;
                                }
                            });
                    cascadingMenuPopWindowStart.showAsDropDown(textview_shifadi, 5, 5);
                } else if (cascadingMenuPopWindowStart != null
                        && cascadingMenuPopWindowStart.isShowing()) {
                    cascadingMenuPopWindowStart.dismiss();
                } else {
                    cascadingMenuPopWindowStart.showAsDropDown(textview_shifadi, 5, 5);
                }
                break;
            case R.id.layout_mudidi:
                if (cascadingMenuPopWindowEnd == null) {
                    //向三级menu添加地区数据
                    cascadingMenuPopWindowEnd = new CascadingMenuPopWindow(this, false);
                    cascadingMenuPopWindowEnd
                            .setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {
                                @Override
                                public void getValue(Area city, String fullname) {
                                    textview_mudidi.setText(fullname);
                                    searchGoods.receivecityid = PoiEntityUtils.convert(city, fullname).getId();
                                    searchGoods.receiveName = fullname;
                                }
                            });
                    cascadingMenuPopWindowEnd.showAsDropDown(textview_mudidi, 5, 5);
                } else if (cascadingMenuPopWindowEnd != null
                        && cascadingMenuPopWindowEnd.isShowing()) {
                    cascadingMenuPopWindowEnd.dismiss();
                } else {
                    cascadingMenuPopWindowEnd.showAsDropDown(textview_mudidi, 5, 5);
                }
                break;
            case R.id.layout_type:
                selectType();
                break;
            case R.id.layout_sort:
                selectSort();
                break;
        }
    }

    private void selectType() {
        AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        builder1.setItems(new String[]{"不限", "重货", "泡货"}, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        textview_type.setText("不限");
                        searchGoods.shippName = "不限";
                        break;
                    case 1:
                        textview_type.setText("重货");
                        searchGoods.shippName = "不限";
                        break;
                    case 2:
                        textview_type.setText("泡货");
                        searchGoods.shippName = "不限";
                        break;
                }
                searchGoods.shipptype = which + 1;
            }
        });
        builder1.create().show();
    }

    private void selectSort() {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(new String[]{"距始发地最近", "距目的地最近"}, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which + 1) {
                    case 1:
                        textview_sort.setText("距始发地最近");
                        searchGoods.rankName = "距始发地最近";
                        break;
                    case 2:
                        textview_sort.setText("距目的地最近");
                        searchGoods.rankName = "距目的地最近";
                        break;
                }
                searchGoods.ranktype = which + 1;

            }
        });
        builder.create().show();
    }


}
